package com.chookss.home.knowledge.adaper;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.text.Spanned;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chookss.MyApp;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.WebCommonActivity;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.base.Token;
import com.chookss.home.document.DocumentPreviewActivity;
import com.chookss.home.document.DownloadService;
import com.chookss.home.entity.DocunmentEntity;
import com.chookss.home.entity.DynamicEntity;
import com.chookss.tools.GlideUtils;
import com.chookss.tools.MyToast;
import com.chookss.tools.NavigationTools;
import com.chookss.tools.PermissionUtils;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.OkGoUtil;
import com.chookss.tools.okgoconfig.callback.JsonCallback;
import com.chookss.tools.okgoconfig.callback.ObserverCallback;
import com.chookss.tools.okgoconfig.model.LzyResponse;
import com.chookss.video.VideoOnePlayActivity;
import com.chookss.video.entity.VideoEntity;
import com.chookss.video.recommend.VedioDetailActivity;
import com.chookss.view.MyImageSpan;
import com.google.gson.reflect.TypeToken;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.johnrambo.ktea.common.klogutil.KLog;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DynamicAdapter extends BaseMultiItemQuickAdapter<DynamicEntity,
        BaseViewHolderEx> {
    private Context mContext;
    private int height, width;
    private String tag;//记录来源

    public DynamicAdapter(Context mContext, List<DynamicEntity> data, String tag) {
        super(data);
        this.mContext = mContext;
        this.tag = tag;
        width = (Utils.getScreenWidthPixel(mContext) - Utils.dip2px(mContext, 38)) / 3;
        height = width * 151 / 210;
//       （1资料，2长视频，3短视频，4新闻，5图文）
        addItemType(1, R.layout.item_dynamic_doc);
        addItemType(2, R.layout.item_dynamic_video);
        addItemType(3, R.layout.item_dynamic_video);
        addItemType(4, R.layout.item_dynamic_news);
        addItemType(5, R.layout.item_dynamic_news);
    }

    @Override
    protected void convert(BaseViewHolderEx helper, DynamicEntity contentBean) {
        switch (helper.getItemViewsType()) {
            //       （1资料，2长视频，3短视频，4新闻，5图文）
            case 1:
                //1--word 2 --excel 3--PPT  4pdf
                ImageView ivType = helper.getView(R.id.ivType);
                TextView tvTitle = helper.getView(R.id.tvTitle);
                TextView tvSize = helper.getView(R.id.tvSize);
                TextView tvName = helper.getView(R.id.tvName);
                TextView tvDate = helper.getView(R.id.tvDate);
                ProgressBar progressBar = helper.getView(R.id.progressBar);
                if (contentBean.getDataBase() == null || "".equals(contentBean.getDataBase())) {
                } else {
                    if (contentBean.getDataBase().getProgress() == 0 || contentBean.getDataBase().getProgress() == 100) {
                        progressBar.setVisibility(View.GONE);
                    } else {
                        progressBar.setVisibility(View.VISIBLE);
                        progressBar.setProgress(contentBean.getDataBase().getProgress());
                    }
                    tvTitle.setText(contentBean.getDataBase().getDataTitle());
                    tvSize.setText(Utils.changeKM(contentBean.getDataBase().getDataSize()));
                    tvName.setText(contentBean.getDataBase().getProvideEmployeeName());
                    tvDate.setText(Utils.getAccurateDate(contentBean.getDataBase().getUploadTime()));
                    if ("1".equals(contentBean.getDataBase().getFileType())) {
                        Glide.with(mContext).load(R.drawable.icon_w).into(ivType);
                    } else if ("2".equals(contentBean.getDataBase().getFileType())) {
                        Glide.with(mContext).load(R.drawable.icon_x).into(ivType);
                    } else if ("3".equals(contentBean.getDataBase().getFileType())) {
                        Glide.with(mContext).load(R.drawable.icon_p).into(ivType);
                    } else if ("4".equals(contentBean.getDataBase().getFileType())) {
                        Glide.with(mContext).load(R.drawable.icon_pdf).into(ivType);
                    } else {
                        Glide.with(mContext).load(R.drawable.icon_file_default).into(ivType);
                    }
                    helper.getItemView().setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (Urls.antiShake.check()) {
                                return;
                            }
                            requestSTORAGE(contentBean.getDataBase().getDataCode(), tvTitle.getText().toString(), helper.getItemPosition(),contentBean.getDataBase().isError());
                        }
                    });
                }
                break;
            case 2:
            case 3:
                ImageView ivVideo = helper.getView(R.id.ivVideo);
                TextView tvVideoTitle = helper.getView(R.id.tvTitle);
                TextView tvVideoName = helper.getView(R.id.tvName);
                TextView tvVideoDate = helper.getView(R.id.tvDate);
                TextView tvTypeVideo = helper.getView(R.id.tvType);
                if(helper.getItemViewsType()==2){
                    tvTypeVideo.setText("视频");
                }else{
                    tvTypeVideo.setText("小视频");
                }
                if (contentBean.getVideo() == null || "".equals(contentBean.getVideo())) {
                } else {
                    tvVideoTitle.setText(contentBean.getVideo().getVideoTitle());
                    tvVideoName.setText(contentBean.getVideo().getProvideEmployeeName());
                    tvVideoDate.setText(Utils.getAccurateDate(contentBean.getVideo().getCreateTime()));
                    GlideUtils.loadCustomRound(mContext, contentBean.getVideo().getVideoCoverPath(), ivVideo, 4);
                    helper.getItemView().setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (Urls.antiShake.check()) {
                                return;
                            }
                            getViedeo(contentBean.getVideo().getVideoId(), helper.getItemViewsType());
                        }
                    });
                }
                break;
            case 4:
                TextView tvNewsTitle = helper.getView(R.id.tvTitle);
                TextView tvNewsName = helper.getView(R.id.tvName);
                TextView tvNewsDate = helper.getView(R.id.tvDate);
                LinearLayout llNewsThreeImg = helper.getView(R.id.llThreeImg);
                LinearLayout llNewsContent = helper.getView(R.id.llContent);
                ImageView ivNewsPic1 = helper.getView(R.id.ivPic1);
                ImageView ivNewsPic2 = helper.getView(R.id.ivPic2);
                ImageView ivNewsPic3 = helper.getView(R.id.ivPic3);
                View placeholderNewsView = helper.getView(R.id.placeholderView);
                View spaceView = helper.getView(R.id.spaceView);
                TextView tvTypeNews = helper.getView(R.id.tvType);
                tvTypeNews.setText("新闻");
                //两个view只能同时存在一个
                ImageView ivNewsOneImg = helper.getView(R.id.ivOneImg);
                if (contentBean.getNews() == null || "".equals(contentBean.getNews())) {
                } else {
                    String title = contentBean.getNews().getNewsTitle().trim().replace("\r|\n", "");
                    if ("1".equals(contentBean.getNews().getIfTop())) {
                        //置顶
                        SpannableString spannableString = new SpannableString("  " + title);
                        Drawable drawable = mContext.getResources().getDrawable(R.drawable.icon_xw_ding);
                        drawable.setBounds(
                                0,
                                0,
                                Utils.dip2px(mContext, 12f),
                                Utils.dip2px(mContext, 12f)
                        );
                        MyImageSpan imageSpan = new MyImageSpan(drawable);
                        spannableString.setSpan(imageSpan, 0, 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                        tvNewsTitle.setText(spannableString);
                    }else{
                        tvNewsTitle.setText(title);
                    }
                    tvNewsName.setText(contentBean.getNews().getEmployeeName());
                    tvNewsDate.setText(Utils.getAccurateDate(contentBean.getNews().getNewsPublishTime()));
                    LinearLayout.LayoutParams llOneImgParams = (LinearLayout.LayoutParams) ivNewsOneImg.getLayoutParams();
                    LinearLayout.LayoutParams ivPic1Params = (LinearLayout.LayoutParams) ivNewsPic1.getLayoutParams();
                    LinearLayout.LayoutParams ivPic2Params = (LinearLayout.LayoutParams) ivNewsPic2.getLayoutParams();
                    LinearLayout.LayoutParams ivPic3Params = (LinearLayout.LayoutParams) ivNewsPic3.getLayoutParams();
                    LinearLayout.LayoutParams llContentParams = (LinearLayout.LayoutParams) llNewsContent.getLayoutParams();
                    if (contentBean.getNews().getPictures().size() > 0) {
                        if (contentBean.getNews().getPictures().size() < 3) {
                            ivNewsOneImg.setVisibility(View.VISIBLE);
                            llNewsThreeImg.setVisibility(View.GONE);
                            llOneImgParams.height = height;
                            llOneImgParams.width = width;
                            ivNewsOneImg.setLayoutParams(llOneImgParams);
                            llContentParams.height = height+Utils.dip2px(mContext,3);
                            placeholderNewsView.setVisibility(View.VISIBLE);
                            spaceView.setVisibility(View.GONE);
                            llNewsContent.setLayoutParams(llContentParams);
                            if (contentBean.getNews().getPictures().get(0).getPicturePath().endsWith("gif")) {
                                GlideUtils.loadCustomRoundGif(mContext, contentBean.getNews().getPictures().get(0).getPicturePath(), ivNewsOneImg, 4);
                            } else {
                                GlideUtils.loadCustomRound(mContext, contentBean.getNews().getPictures().get(0).getPicturePath(), ivNewsOneImg, 4);
                            }
                        } else {
                            ivNewsOneImg.setVisibility(View.GONE);
                            llNewsThreeImg.setVisibility(View.VISIBLE);
                            ivPic1Params.height = height;
                            ivPic1Params.width = width;
                            ivNewsPic1.setLayoutParams(ivPic1Params);
                            ivPic2Params.height = height;
                            ivPic2Params.width = width;
                            ivNewsPic2.setLayoutParams(ivPic2Params);
                            ivPic3Params.height = height;
                            ivPic3Params.width = width;
                            ivNewsPic3.setLayoutParams(ivPic3Params);
                            llContentParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
                            placeholderNewsView.setVisibility(View.GONE);
                            spaceView.setVisibility(View.VISIBLE);
                            llNewsContent.setLayoutParams(llContentParams);
                            if (contentBean.getNews().getPictures().get(0).getPicturePath().endsWith("gif")) {
                                GlideUtils.loadCustomRoundGif(mContext, contentBean.getNews().getPictures().get(0).getPicturePath(), ivNewsPic1, 4);
                            } else {
                                GlideUtils.loadCustomRound(mContext, contentBean.getNews().getPictures().get(0).getPicturePath(), ivNewsPic1, 4);
                            }
                            if (contentBean.getNews().getPictures().get(1).getPicturePath().endsWith("gif")) {
                                GlideUtils.loadCustomRoundGif(mContext, contentBean.getNews().getPictures().get(1).getPicturePath(), ivNewsPic2, 4);
                            } else {
                                GlideUtils.loadCustomRound(mContext, contentBean.getNews().getPictures().get(1).getPicturePath(), ivNewsPic2, 4);
                            }
                            if (contentBean.getNews().getPictures().get(2).getPicturePath().endsWith("gif")) {
                                GlideUtils.loadCustomRoundGif(mContext, contentBean.getNews().getPictures().get(2).getPicturePath(), ivNewsPic3, 4);
                            } else {
                                GlideUtils.loadCustomRound(mContext, contentBean.getNews().getPictures().get(2).getPicturePath(), ivNewsPic3, 4);
                            }
                        }
                    } else {
                        ivNewsOneImg.setVisibility(View.GONE);
                        llNewsThreeImg.setVisibility(View.GONE);
                        placeholderNewsView.setVisibility(View.GONE);
                        spaceView.setVisibility(View.VISIBLE);
                        llContentParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
                        llNewsContent.setLayoutParams(llContentParams);
                    }

                    helper.getItemView().setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (Urls.antiShake.check()) {
                                return;
                            }
                            checkNewsData(contentBean);

                        }
                    });
                }
                break;
            case 5:
                TextView tvImageTextTitle = helper.getView(R.id.tvTitle);
                TextView tvImageTextName = helper.getView(R.id.tvName);
                TextView tvImageTextDate = helper.getView(R.id.tvDate);
                LinearLayout llImageTextThreeImg = helper.getView(R.id.llThreeImg);
                LinearLayout llImageTextContent = helper.getView(R.id.llContent);
                ImageView ivImageTextPic1 = helper.getView(R.id.ivPic1);
                ImageView ivImageTextPic2 = helper.getView(R.id.ivPic2);
                ImageView ivImageTextPic3 = helper.getView(R.id.ivPic3);
                View placeholderImageTextView = helper.getView(R.id.placeholderView);
                View llImageTextSpaceView = helper.getView(R.id.spaceView);
                TextView tvTypeImageTextView = helper.getView(R.id.tvType);
                tvTypeImageTextView.setText("图文");
                //两个view只能同时存在一个
                ImageView ivImageTextOneImg = helper.getView(R.id.ivOneImg);
                if (contentBean.getImageText() == null || "".equals(contentBean.getImageText())) {
                } else {
                    tvImageTextTitle.setText(contentBean.getImageText().getTitle());
                    tvImageTextName.setText(contentBean.getImageText().getEmployeeName());
                    tvImageTextDate.setText(Utils.getAccurateDate(contentBean.getImageText().getCreateTime()));
                    LinearLayout.LayoutParams llOneImgParams = (LinearLayout.LayoutParams) ivImageTextOneImg.getLayoutParams();
                    LinearLayout.LayoutParams ivPic1Params = (LinearLayout.LayoutParams) ivImageTextPic1.getLayoutParams();
                    LinearLayout.LayoutParams ivPic2Params = (LinearLayout.LayoutParams) ivImageTextPic2.getLayoutParams();
                    LinearLayout.LayoutParams ivPic3Params = (LinearLayout.LayoutParams) ivImageTextPic3.getLayoutParams();
                    LinearLayout.LayoutParams llContentParams = (LinearLayout.LayoutParams) llImageTextContent.getLayoutParams();
                    if (contentBean.getImageText().getPictureList().size() > 0) {
                        if (contentBean.getImageText().getPictureList().size() < 3) {
                            ivImageTextOneImg.setVisibility(View.VISIBLE);
                            llImageTextThreeImg.setVisibility(View.GONE);
                            llOneImgParams.height = height;
                            llOneImgParams.width = width;
                            ivImageTextOneImg.setLayoutParams(llOneImgParams);
                            llContentParams.height = height+Utils.dip2px(mContext,3);
                            placeholderImageTextView.setVisibility(View.VISIBLE);
                            llImageTextSpaceView.setVisibility(View.GONE);
                            llImageTextContent.setLayoutParams(llContentParams);
                            if (contentBean.getImageText().getPictureList().get(0).endsWith("gif")) {
                                GlideUtils.loadCustomRoundGif(mContext, contentBean.getImageText().getPictureList().get(0), ivImageTextOneImg, 4);
                            } else {
                                GlideUtils.loadCustomRound(mContext, contentBean.getImageText().getPictureList().get(0), ivImageTextOneImg, 4);
                            }
                        } else {
                            ivImageTextOneImg.setVisibility(View.GONE);
                            llImageTextThreeImg.setVisibility(View.VISIBLE);
                            ivPic1Params.height = height;
                            ivPic1Params.width = width;
                            ivImageTextPic1.setLayoutParams(ivPic1Params);
                            ivPic2Params.height = height;
                            ivPic2Params.width = width;
                            ivImageTextPic2.setLayoutParams(ivPic2Params);
                            ivPic3Params.height = height;
                            ivPic3Params.width = width;
                            ivImageTextPic3.setLayoutParams(ivPic3Params);
                            llContentParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
                            placeholderImageTextView.setVisibility(View.GONE);
                            llImageTextSpaceView.setVisibility(View.VISIBLE);
                            llImageTextContent.setLayoutParams(llContentParams);
                            if (contentBean.getImageText().getPictureList().get(0).endsWith("gif")) {
                                GlideUtils.loadCustomRoundGif(mContext, contentBean.getImageText().getPictureList().get(0), ivImageTextPic1, 4);
                            } else {
                                GlideUtils.loadCustomRound(mContext, contentBean.getImageText().getPictureList().get(0), ivImageTextPic1, 4);
                            }
                            if (contentBean.getImageText().getPictureList().get(1).endsWith("gif")) {
                                GlideUtils.loadCustomRoundGif(mContext, contentBean.getImageText().getPictureList().get(1), ivImageTextPic2, 4);
                            } else {
                                GlideUtils.loadCustomRound(mContext, contentBean.getImageText().getPictureList().get(1), ivImageTextPic2, 4);
                            }
                            if (contentBean.getImageText().getPictureList().get(2).endsWith("gif")) {
                                GlideUtils.loadCustomRoundGif(mContext, contentBean.getImageText().getPictureList().get(2), ivImageTextPic3, 4);
                            } else {
                                GlideUtils.loadCustomRound(mContext, contentBean.getImageText().getPictureList().get(2), ivImageTextPic3, 4);
                            }
                        }
                    } else {
                        ivImageTextOneImg.setVisibility(View.GONE);
                        llImageTextThreeImg.setVisibility(View.GONE);
                        placeholderImageTextView.setVisibility(View.GONE);
                        llImageTextSpaceView.setVisibility(View.VISIBLE);
                        llContentParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
                        llImageTextContent.setLayoutParams(llContentParams);
                    }

                    helper.getItemView().setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (Urls.antiShake.check()) {
                                return;
                            }
                            checkData(contentBean);
                        }
                    });
                }
                break;

        }

    }

    private void getViedeo(String id, int type) {
        Map<String, String> map = new HashMap<>();
        map.put("videoId", id);
        MyHttpRequest.getDefault().getRequest(new TypeToken<LzyResponse<VideoEntity>>() {
        }.getType(), null, Urls.selectAPPVideoById, SignUtils2.makeSignStr(map), false, new ObserverCallback<VideoEntity>() {
            @Override
            public void onSuccess(VideoEntity data) {
                if (Utils.isNull(data.videoId)) {
                    MyToast.show("视频id为空");
                    return;
                }
                if (type == 2) {
                    Intent intent = new Intent(mContext, VedioDetailActivity.class);
                    intent.putExtra("video", data);
                    mContext.startActivity(intent);
                } else {
                    Intent intent = new Intent(mContext, VideoOnePlayActivity.class);
                    intent.putExtra("watermark", "2");
                    intent.putExtra("video", data);
                    mContext.startActivity(intent);
                }
            }

            @Override
            public void onError(String errorMsg) {
                MyToast.show(Utils.onErrorTips(errorMsg));
            }
        });
    }

    private void toClickNum(String newsId) {
        Map<String, String> map = new HashMap<>();
        map.put("id", newsId);
        MyHttpRequest.getDefault().postRequestCompat(null, Urls.setClickNum, SignUtils2.makeSignStr(map), false, new ObserverCallback<String>() {
            @Override
            public void onSuccess(String data) throws Exception {
            }

            @Override
            public void onError(String errorMsg) {
                MyToast.show(Utils.onErrorTips(errorMsg));
            }
        });
    }
    /**
     * 判断数据是否被下架或者是否有权限
     */
    private void checkData(DynamicEntity contentBean) {
        Map<String, String> map = new HashMap<>();
//        （1资料，2长视频，3短视频，4新闻，5图文）
        map.put("resourceId", contentBean.getImageText().getMainCode());
        map.put("resourceType", "5");
        MyHttpRequest.postRequest(Urls.checkData, this, SignUtils2.makeSignStr(map), new JsonCallback<String>() {
            @Override
            public void onSuccess(Response<String> response) {
                try {
                    JSONObject jsonObject = new JSONObject(response.body().toString());
                    if ("200".equals(jsonObject.getString("code"))) {
                        NavigationTools.saveUseRecord(mContext, "1", "17");
                        String addressType = "";
                        if (Urls.baseUrl.contains("dev_kmer")) {
                            addressType = "1";
                        } else {
                            addressType = "0";
                        }
                        String url = Urls.tuwenDetail + "mainCode=" + contentBean.getImageText().getMainCode() + "&token=" + Token.INSTANCE.getToken() + "&statusHeight=" + Utils.getStatusBarHeightDp(mContext) + "&time=" + System.currentTimeMillis() + "&addressType=" + addressType;
                        Intent intent = new Intent(mContext, WebCommonActivity.class);
                        intent.putExtra("url", url);
                        intent.putExtra("title", "详情");
                        if ("0".equals(contentBean.getImageText().getStatusCd())) {
                            intent.putExtra("isTuwen", 2);
                        } else if ("1".equals(contentBean.getImageText().getStatusCd()) || "3".equals(contentBean.getImageText().getStatusCd())) {
                        } else {
                            intent.putExtra("isTuwen", 3);
                        }
                        intent.putExtra("waterMark", "13");
                        intent.putExtra("url", url);
                        intent.putExtra("newsTitle", contentBean.getImageText().getTitle());
                        intent.putExtra("createEmployeeName", contentBean.getImageText().getCreateEmployeeName());
                        intent.putExtra("resourceId", contentBean.getImageText().getMainCode());
                        intent.putExtra("newsContent", "");
                        if (contentBean.getImageText().getPictureList().size() > 0) {
                            if (Utils.isNull(contentBean.getImageText().getPictureList().get(0))) {
                                intent.putExtra("picUrl", contentBean.getImageText().getPictureList().get(0));
                            } else {
                                intent.putExtra("picUrl", contentBean.getImageText().getPictureList().get(0));
                            }
                        } else {
                            intent.putExtra("picUrl", "");
                        }
                        mContext.startActivity(intent);
                    } else {
                        if (jsonObject.getString("code").startsWith("400")) {
                            OkGoUtil.onErrorIntent(jsonObject.getString("code"),jsonObject.getString("msg"));
                        }else{
                            MyToast.show(jsonObject.getString("msg"));
                        }
                    }

                } catch (
                        JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    /**
     * 判断数据是否被下架或者是否有权限
     */
    private void checkNewsData(DynamicEntity contentBean) {
        Map<String, String> map = new HashMap<>();
//        （1资料，2长视频，3短视频，4新闻，5图文）
        map.put("resourceId", contentBean.getNews().getNewsCode());
        map.put("resourceType", "4");
        MyHttpRequest.postRequest(Urls.checkData, this, SignUtils2.makeSignStr(map), new JsonCallback<String>() {
            @Override
            public void onSuccess(Response<String> response) {
                try {
                    JSONObject jsonObject = new JSONObject(response.body().toString());
                    if ("200".equals(jsonObject.getString("code"))) {
                        String addressType = "";
                        if (Urls.baseUrl.contains("dev_kmer")) {
                            addressType = "1";
                        } else {
                            addressType = "0";
                        }
                        Intent intent = new Intent(mContext, WebCommonActivity.class);
                        String url = "";
                        if (!Utils.isNull(contentBean.getNews().getQuoteCode()) && "1".equals(contentBean.getNews().getQuoteFlag())) {
                            url = Urls.tuwenDetail + "mainCode=" + contentBean.getNews().getQuoteCode() +"&newsCode=" + contentBean.getNews().getNewsCode()+ "&token=" + Token.INSTANCE.getToken() + "&statusHeight=" + Utils.getStatusBarHeightDp(mContext) + "&time=" + System.currentTimeMillis() + "&addressType=" + addressType;
                        } else if ("2".equals(contentBean.getNews().getQuoteFlag())) {
                            url = contentBean.getNews().getNewsContent();
                        } else {
                            url = Urls.webBaseUrl + "indexNew.html#/newDetail?newsCode=" + contentBean.getNews().getNewsCode() + "&token=" + Token.INSTANCE.getToken() + "&statusHeight=" + Utils.getStatusBarHeightDp(mContext) + "&time=" + System.currentTimeMillis() + "&addressType=" + addressType;
                        }
                        KLog.i(url);
                        intent.putExtra("isShare", true);
                        intent.putExtra("resourceId", contentBean.getNews().getNewsCode());
                        intent.putExtra("newsContent", contentBean.getNews().getNewsContent());
                        intent.putExtra("createEmployeeName", contentBean.getNews().getEmployeeName());
                        intent.putExtra("url", url);
                        intent.putExtra("newsTitle", contentBean.getNews().getNewsTitle());
                        if (contentBean.getNews().getPictures().size() > 0) {
                            if (Utils.isNull(contentBean.getNews().getPictures().get(0).getThumbnailPath())) {
                                intent.putExtra("picUrl", contentBean.getNews().getPictures().get(0).getPicturePath());
                            } else {
                                intent.putExtra("picUrl", contentBean.getNews().getPictures().get(0).getThumbnailPath());
                            }
                        } else {
                            intent.putExtra("picUrl", "");
                        }
                        intent.putExtra("waterMark", "13");
                        mContext.startActivity(intent);
//                        if (!Utils.isNull(contentBean.getNews().getId())) {
//                            toClickNum(contentBean.getNews().getId());
//                        }
                    } else {
                        if (jsonObject.getString("code").startsWith("400")) {
                            OkGoUtil.onErrorIntent(jsonObject.getString("code"),jsonObject.getString("msg"));
                        }else{
                            MyToast.show(jsonObject.getString("msg"));
                        }
                    }

                } catch (
                        JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    private void requestSTORAGE(String dataCode, String retitle, int rePosition,boolean isError) {
        XXPermissions.with(mContext)
                .permission(Permission.Group.STORAGE)
                .request(new OnPermissionCallback() {
                    @Override
                    public void onGranted(List<String> granted, boolean all) {
                        if (all) {
                            getDocunmentDetail(dataCode, retitle, rePosition,isError);
                        } else {
                            MyToast.show("存储未被授权，请先去权限设置里面授权");
                        }
                    }

                    @Override
                    public void onDenied(List<String> denied, boolean quick) {
                        PermissionUtils.errorPermission(mContext, denied, quick);
                    }
                });
    }

    private void getDocunmentDetail(String dataCode, String docTitle, int docPosition,boolean isError) {
        Map<String, String> map = new HashMap<>();
        map.put("dataCode", dataCode);
        MyHttpRequest.getDefault().getRequest(new TypeToken<LzyResponse<DocunmentEntity>>() {
        }.getType(), null, Urls.getDetail, SignUtils2.makeSignStr(map), false, new ObserverCallback<DocunmentEntity>() {
            @Override
            public void onSuccess(DocunmentEntity newData) {
                if (newData != null) {
                    openFile(newData, docTitle, docPosition,isError);
                }
            }

            @Override
            public void onError(String errorMsg) {
                MyToast.show(Utils.onErrorTips(errorMsg));
            }
        });
    }

    private void openFile(DocunmentEntity entity, String title, int position,boolean isError) {
        if (!Utils.isNull(entity.getDataUrl())) {
            String url, name, fileUrl, urls;
            url = entity.getDataUrl();
            urls = url.split("\\?")[0];
            fileUrl = urls.substring(urls.lastIndexOf("/") + 1, urls.length());
            if (MyApp.downloadFiles.contains(title)||!MyApp.mIsInitTBSSuccess||isError) {
                if (!MyApp.docunmentOpen) {
                    Intent intent = new Intent(mContext, DocumentPreviewActivity.class);
                    intent.putExtra("fileUrl", fileUrl);
                    intent.putExtra("fileName", title);
                    intent.putExtra("dataCode", entity.getDataCode());
                    mContext.startActivity(intent);
                }
            } else {
//                    downTask(url, title, entity, position);
                entity.setProgress(1);
                notifyItemChanged(position);
                Intent intent = new Intent(mContext, DownloadService.class);
                intent.putExtra("fileUrl", fileUrl);
                intent.putExtra("fileName", title);
                intent.putExtra("url", url);
                intent.putExtra("dataCode", entity.getDataCode());
                intent.putExtra("position", position);
                intent.putExtra("tag", tag);
                mContext.startService(intent);
            }

        }
    }

}
